
clear
set more off
set logtype text
set matsize 800
cap log close
clear matrix




use "usa_00041.dta"   
global FE_LEVEL  "metarea"
global FE_NAME   "METRO"
global DEMOG_DATA  "METRO_demog_data"
global GENDER_SELECT  "sex==1"




log using prepare_men, replace
 


 /* Download from IPUMS  ACS 3-YR 2011, 2000 5%, 1990 5% State, 1980 5% State, 1970 1% Metro fm1, 1970 Metro fm2, 1960 5%    */

keep   year  cpi99  statefip  metarea  gq  ownershp  ownershpd  valueh  perwt  nchild  nchlt5  sex  age  marst  bpl  racesing  school  educ  educd  gradeatt  gradeattd  schltype  empstat  empstatd  labforce  occ1990  ind1990  classwkr  classwkrd  wkswork1  wkswork2  hrswork1  hrswork2  uhrswork  absent  workedyr  incwage  incwelfr  incsupp  poverty  marst_mom  marst_pop  racesing_mom  racesing_pop 
 

replace year=2010 if  year==2011         

replace perwt = perwt/2  if year==1970   /* since there are two 1 percent samples for 1970  */

gen male   = sex==1
gen female = sex==2

gen white = racesing==1
gen black = racesing==2

gen immigrant =(bpl>=100)
gen native = immigrant==0


*************************************************************************************************************************************************

preserve

keep if native==1

keep  statefip  bpl  metarea   year   perwt  male female white black immigrant native  poverty marst_mom marst_pop  gq  racesing age sex educ  nchild  empstatd school  gradeatt  


gen poor  =  poverty<=100  if poverty!=0
gen        CHILD_poor =  poor   if native==1 &  age<=12  &  racesing==1
gen  black_CHILD_poor =  poor   if native==1 &  age<=12  &  racesing==2
drop poor 

gen      one_parent =  (marst_mom>=3  &  marst_mom<=6) |  (marst_pop>=3  &  marst_pop<=6)   if marst_mom!=.  
replace one_parent  =  1  if marst_mom==.  &  marst_pop~=.
replace one_parent  =  1  if marst_mom~=.  &  marst_pop==.
replace one_parent  =  1  if marst_mom==.  &  marst_pop==.
replace one_parent  =  .  if age>=16

gen         CHILD_one_parent  =  one_parent         if native==1 &  age<=12  &  racesing==1
gen   black_CHILD_one_parent  =  one_parent         if native==1 &  age<=12  &  racesing==2
drop one_parent marst_*  *_mom    


gen  studying = school==2 if school~=.                                        
replace studying = 0  if gradeatt==0  &  year==1970  & school==.
replace studying = 1  if gradeatt~=0  &  year==1970  & school==.
drop school  gradeatt


gen       teen_mom_1 =  nchild>0          if white==1 &  sex==2  &  age>=15 & age<20
gen black_teen_mom_1 =  nchild>0          if black==1 &  sex==2  &  age>=15 & age<20


gen  x_1 = perwt  if white==1 &  age>=25 & age<=55
gen  x_2 = perwt  if black==1 &  age>=25 & age<=55

egen       pop = total(x_1), by($FE_LEVEL year)
egen black_pop = total(x_2), by($FE_LEVEL year)
drop x_*

egen  metro_statefip = mode(statefip), by(metarea)

 
collapse  black_*    CHILD_*   teen_*  pop  metro_statefip  [aw=perwt], by($FE_LEVEL year)

sort $FE_LEVEL year
save  "$DEMOG_DATA", replace


restore


*************************************************************************************************************************************************

keep if $GENDER_SELECT
keep if age>=25 & age<=55

*************************************************************************************************************************************************



gen nevermar      =  marst==6
gen married_1     = (marst==1 | marst==2)                                      /* including spouse not present (marst==2)   */
gen divorced_1    = (marst==3 | marst==4)  if marst~=5 & marst~=6              /* separated or divorced among those ever married but not widowed  */
gen widowed_1     =  marst==5                                                  /* among all people               */
gen widowed_2     =  marst==5   if marst~=6                                    /* among those that were married  */

gen  selfemp =(classwkr==1)
gen  worked  = (wkswork2>=1 & wkswork2<=6)                                     /*  Worked more than zero weeks last year  */

egen hrswork_temp=mean(hrswork1),  by(hrswork2)
gen hrswork=uhrswork
replace hrswork=hrswork_temp if year==1970 | year==1960




gen  studying = school==2 if school~=.                                         
replace studying = 0  if gradeatt==0  &  year==1970  & school==.
replace studying = 1  if gradeatt~=0  &  year==1970  & school==.


rename ind1990  IND1990
rename occ1990  OCC1990

gen  manufacturing   = (IND1990>=100 & IND1990<400 & hrswork>=20)    if          studying~=1
gen blue_collar      = (OCC1990>500 & OCC1990<900  & hrswork>=20)    if          studying~=1
gen white_collar     = (OCC1990<500                & hrswork>=20)    if          studying~=1



gen service_industry=(IND1990==12 | IND1990==20 | IND1990==30 | (IND1990>=400 & IND1990<=432) | (IND1990>=440 & IND1990<=442) | ///
                     (IND1990>=450 & IND1990<=472) | IND1990==621| (IND1990>=700 & IND1990<=712) | (IND1990>=721 & IND1990<=760) | (IND1990>=761 & IND1990<=791) | ///
                     (IND1990>=800 & IND1990<=810) | (IND1990>=812 & IND1990<=893) ) & hrswork>=20
gen  services  =  service_industry   if           studying~=1 





 
* OCCUPATION GROUPS


gen     occ_group = 1  if   OCC1990<=22 					/*  Executive, Administrative, and Managerial Occupations*/
replace occ_group = 2  if   OCC1990>22  & OCC1990<=37		/*  Management Related Occupations*/
replace occ_group = 3  if   OCC1990>43  & OCC1990<=59		/*  Engineers, Architechts, and Surveyors*/
replace occ_group = 4  if   OCC1990>59  & OCC1990<=68		/*  Mathematical and Computer Scientists*/
replace occ_group = 5  if   OCC1990>68  & OCC1990<=83		/*  Natural Scientists*/
replace occ_group = 6  if   OCC1990>83  & OCC1990<=89 		/*  Health Diagnosing Occupations*/
replace occ_group = 7  if   OCC1990>89  & OCC1990<=97		/*  Health Assessment and Treating Occupations*/
replace occ_group = 8  if   OCC1990>97  & OCC1990<=106		/*  Therapists*/
replace occ_group = 9  if   OCC1990>106 & OCC1990<=154	    /*  Teachers, Postsecondary*/
replace occ_group = 10 if   OCC1990>154 & OCC1990<=163	    /*  Teachers, Except Postsecondary*/
replace occ_group = 11 if   OCC1990>163 & OCC1990<=165	    /*  Librarians, Archivists, and Curators*/
replace occ_group = 12 if   OCC1990>165 & OCC1990<=173  	/*  Social Scientists and Urban Planners*/
replace occ_group = 13 if   OCC1990>173 & OCC1990<=176 	    /*  Social, Recreation, and Religious Workers*/
replace occ_group = 14 if   OCC1990>176 & OCC1990<=179	    /*  Lawyers and Judges*/
replace occ_group = 15 if   OCC1990>179 & OCC1990<=200	    /*  Writers, Artists, Entertainers, and Athletes*/
replace occ_group = 16 if   OCC1990>200 & OCC1990<=208	    /*  Health Technologists and Technicians*/
replace occ_group = 17 if   OCC1990>208 & OCC1990<=223	    /*  Engineering and Related Technologists and Technicians*/
replace occ_group = 18 if   OCC1990>223 & OCC1990<=235	    /*  Technicians, Except Health, Engineering, and Science*/
replace occ_group = 19 if   OCC1990>235 & OCC1990<=303 	    /*  Sales Occupations*/
replace occ_group = 20 if   OCC1990>303 & OCC1990<=315	    /*  Computer Equipment Operators*/
replace occ_group = 21 if   OCC1990>315 & OCC1990<=344	    /*  Information Clerks*/
replace occ_group = 22 if   OCC1990>344 & OCC1990<=347	    /*  Duplicating, Mail, and Other Office Machine Operators*/
replace occ_group = 23 if   OCC1990>347 & OCC1990<=357      /*  Communications Equipment Operators*/
replace occ_group = 24 if   OCC1990>357 & OCC1990<=373	    /*  Material Recording, Scheduling, and Distributing Clerks*/
replace occ_group = 25 if   OCC1990>373 & OCC1990<=389	    /*  Adjusters and Investigators*/
replace occ_group = 26 if   OCC1990>389 & OCC1990<=469	    /*  SERVICE OCCUPATIONS*/
replace occ_group = 27 if   OCC1990>469 & OCC1990<=498 	    /*  FARMING, FORESTRY, AND FISHING OCCUPATIONS*/
replace occ_group = 28 if   OCC1990>498 & OCC1990<=699	    /*  PRECISION PRODUCTION, CRAFT, AND REPAIR OCCUPATIONS*/
replace occ_group = 29 if   OCC1990>699 & OCC1990<=889	    /*	OPERATORS, FABRICATORS, AND LABORERS*/
replace occ_group = 30 if   OCC1990>889 & OCC1990<=905	    /*  MILITARY OCCUPATIONS*/




 

* INDUSTRY GROUPS

gen     ind_group = 1   if    IND1990>=10 & IND1990<=32 		/*  AGRICULTURE, FORESTRY, AND FISHERIES*/
replace ind_group = 2   if    IND1990>32  & IND1990<=50 		/*	MINING*/
replace ind_group = 3   if    IND1990>50  & IND1990<=60      	/*	CONSTRUCTION*/
replace ind_group = 4   if    IND1990>60  & IND1990<=392		/*  MANUFACTURING*/
replace ind_group = 5   if    IND1990>392 & IND1990<=472		/*	TRANSPORTATION, COMMUNICATIONS, AND OTHER PUBLIC UTILITIES*/
replace ind_group = 6   if    IND1990>472 & IND1990<=571		/*	WHOLESALE TRADE*/
replace ind_group = 7   if    IND1990>571 & IND1990<=691		/*	RETAIL TRADE*/
replace ind_group = 8   if    IND1990>691 & IND1990<=712		/*	FINANCE, INSURANCE, AND REAL ESTATE*/
replace ind_group = 9   if    IND1990>712 & IND1990<=760		/*	BUSINESS AND REPAIR SERVICES*/
replace ind_group = 10  if    IND1990>760 & IND1990<=791		/*	PERSONAL SERVICES*/
replace ind_group = 11  if    IND1990>791 & IND1990<=810		/*	ENTERTAINMENT AND RECREATION SERVICES*/
replace ind_group = 12  if    IND1990>810 & IND1990<=893		/*	PROFESSIONAL AND RELATED SERVICES*/
replace ind_group = 13  if    IND1990>893 & IND1990<=932		/*	PUBLIC ADMINISTRATION*/




*************************************************
*  Industries within MFG for the Bartik MFG IV
*************************************************
gen  industry_temp="."
replace industry_temp="Nondurable Goods" 									 if   IND1990>=100 & IND1990<=130		/*  Nondurable Goods*/
replace industry_temp="Textile mill products" 								 if   IND1990>=132 & IND1990<=150		/*  Textile mill products*/
replace industry_temp="Apparel and other finished textile products" 		 if   IND1990>=151 & IND1990<=152 		/*	Apparel and other finished textile products*/
replace industry_temp="Paper and allied products" 							 if   IND1990>=160 & IND1990<=162 		/*	Paper and allied products*/
replace industry_temp="Printing, publishing, and allied industries" 		 if   IND1990>=171 & IND1990<=172 		/*	Printing, publishing, and allied industries*/
replace industry_temp="Chemicals and allied products" 						 if   IND1990>=180 & IND1990<=192 		/*	Chemicals and allied products*/
replace industry_temp="Petroleum and coal products"							 if   IND1990>=200 & IND1990<=201		/*	Petroleum and coal products*/
replace industry_temp="Rubber and miscellaneous plastics products"			 if   IND1990>=210 & IND1990<=212 	    /*	Rubber and miscellaneous plastics products*/
replace industry_temp="Leather and leather products"						 if   IND1990>=220 & IND1990<=222 	    /*	Leather and leather products*/
replace industry_temp="Lumber and woods products, except furniture" 		 if   IND1990>230  & IND1990<=242		/*  Lumber and woods products, except furniture*/
replace industry_temp="Stone, clay, glass and concrete products"			 if   IND1990>=250 & IND1990<=262		/*	Stone, clay, glass and concrete products*/
replace industry_temp="Metal industries" 									 if   IND1990>=270 & IND1990<=301		/*	Metal industries*/
replace industry_temp="Machinery and computing equipment"					 if   IND1990>=310 & IND1990<=332		/*	Machinery and computing equipment*/
replace industry_temp="Electrical machinery, equipment, and supplies" 		 if   IND1990>340  & IND1990<=350		/*	Electrical machinery, equipment, and supplies*/
replace industry_temp="Transportation equipment" 							 if   IND1990>351  & IND1990<=370		/*	Transportation equipment*/
replace industry_temp="Professional and photographic equipment, and watches" if   IND1990>371  & IND1990<=392		/*	Professional and photographic equipment, and watches*/
encode industry_temp, generate (industry)
bysort year:  tab industry, missing



*************************************************************************************************************************************************





*  Replacing top coding

replace incwage = .  if incwage== 999999     /*  N/A        */
replace incwage = .  if incwage== 999998     /*  missing    */
 
replace incwage=incwage*1.5  if year==1960 & incwage==25000
replace incwage=incwage*1.5  if year==1970 & incwage==50000
replace incwage=incwage*1.5  if year==1980 & incwage==75000
replace incwage = incwage*cpi99
gen lincwage = log(incwage)  if incwage~=. & incwage~=0

 

replace valueh = . if valueh==0 | valueh==9999998 | valueh==9999999
replace valueh=valueh*1.5  if year==1960 & valueh==35000
replace valueh=valueh*1.5  if year==1970 & valueh==50000
replace valueh=valueh*1.5  if year==1980 & valueh==200000
replace valueh=valueh*1.5  if year==1990 & valueh==400000
replace valueh=valueh*1.5  if year==2000 & valueh==1000000   
replace valueh=valueh*cpi99
gen lvalueh = log(valueh) if valueh~=0


gen home_owner = ownershp==1



gen       welfare_inc    =  incwelfr                if year>=1970    
replace   welfare_inc    =  incwelfr + incsupp      if year>=2000    
gen       welfare_dummy  =  welfare_inc > 0         if year>=1970
drop      incwelfr  incsupp

gen poor  =  poverty<=100  if poverty~=0 

drop ownershp  poverty  



gen     educ_n=.
replace educ_n= . if educ==0
replace educ_n=2  if educ==1
replace educ_n=7  if educ==2
replace educ_n=9  if educ==3
replace educ_n=10 if educ==4
replace educ_n=11 if educ==5
replace educ_n=12 if educ==6
replace educ_n=13 if educ==7
replace educ_n=14 if educ==8
replace educ_n=15 if educ==9
replace educ_n=16 if educ==10
replace educ_n=18.5 if educ==11

gen age_25_34=(age>=25 & age<=34) 
gen age_35_44=(age>=35 & age<=44) 
gen age_45_55=(age>=45 & age<=55) 

gen HSD=(educ_n<12) 
gen HSG=(educ_n==12)
gen COD= (educ_n>12 & educ_n<16)
gen COG=(educ_n>=16)
gen NONCOG=(educ_n<16)

gen     educ_group = 1  if  HSD==1
replace educ_group = 2  if  HSG==1
replace educ_group = 3  if  COD==1
replace educ_group = 4  if  COG==1


gen     age_group = 1  if  (age>=25 & age<30)
replace age_group = 2  if  (age>=30 & age<35)
replace age_group = 3  if  (age>=35 & age<40)
replace age_group = 4  if  (age>=40 & age<45)
replace age_group = 5  if  (age>=45 & age<50)


gen emprate            = (hrswork>=30)   |  studying==1                         if             gq==1  
gen emprate_noncog     = (hrswork>=30)   |  studying==1                         if NONCOG==1 & gq==1  
gen emprate_cog        = (hrswork>=30)   |  studying==1                         if COG==1    & gq==1  





*************************************************************************************************************************************************



drop if empstatd==14                   /* Dropping those Armed forces--at work*/



replace nevermar     = .  if native==0
replace married_1    = .  if native==0  
replace divorced_1   = .  if native==0  
gen married_2        =  married_1   if native==1  &  gq==1
gen divorced_2       =  divorced_1  if native==1  &  gq==1

replace widowed_1    = .  if native==0
replace widowed_2    = .  if native==0

gen single_mom           =  marst~=1 & nchild>0          if sex==2

gen  mfg = manufacturing                                 if native==1
replace  services   = .                                  if immigrant==1


replace  immigrant  =  . if gq~=1
replace  welfare_dummy   = .                   if native==0
replace  home_owner      = .                   if native==0
gen      groupq         =    gq~=1             if native==1    

replace  lvalueh =  .  if   immigrant==1
replace poor     =  .  if   immigrant==1





replace HSD = .  if  immigrant==1 
replace HSG = .  if  immigrant==1 
replace COD = .  if  immigrant==1 
replace COG = .  if  immigrant==1 

replace age_25_34 = .  if  immigrant==1 
replace age_35_44 = .  if  immigrant==1 
replace age_45_55 = .  if  immigrant==1 

replace emprate            = .  if immigrant==1
replace emprate_noncog     = .  if immigrant==1
replace emprate_cog        = .  if immigrant==1




sum incwage, det
replace lincwage =  .  if native==0 | wkswork2~=6 | hrswork<35  | gq~=1  | studying==1  | selfemp==1  /* native, working 50-52 weeks, at least 35 hrs per week, not in group quarters, not a student or self-employed */
sum lincwage, det
replace lincwage = .  if incwage<4160                                                                 /*  less than two dollars an hour for 40 hrs a week and 52 weeks, min wage was 5.15 in 1999  */
sum lincwage, det



global  VARBLIST          "married_1  married_2  nevermar  divorced_1   widowed_2    poor  welfare_dummy  home_owner    groupq    emprate     emprate_noncog   emprate_cog    lincwage  lvalueh"
global  VARBLIST_FEMALES  "single_mom    "
global  VARB_DEMOS        "mfg   services       HSD HSG COD COG  educ_n  age_25_34  age_35_44  age_45_55"





*************************************************************************************************************************************************




****************************
***  FIGURES FOR MEN     ***
****************************

preserve
keep if sex==1
keep if native==1
drop if black==0 & white==0
gen      white_ror_lincwage = .
gen      black_ror_lincwage = .
gen      white_residual     = .
gen      black_residual     = .


foreach DECADE of numlist 1960 1970 1980 1990 2000 2010  {

ereturn clear                                                   

cap noi   regress  lincwage          educ_n  age  i.age_group            [aw=perwt]             if    year==`DECADE'  & white==1
cap noi   replace              white_ror_lincwage =  _b[educ_n]                                 if    year==`DECADE'  & white==1 
predict                        x1                                                               if    year==`DECADE'  & white==1  &  e(sample),  residual
replace                        white_residual = x1                                              if    year==`DECADE'  & white==1  &  e(sample)
ereturn clear                                                   
drop x1

cap noi   regress  lincwage         educ_n  age  i.age_group             [aw=perwt]             if    year==`DECADE'  & black==1
cap noi   replace              black_ror_lincwage =  _b[educ_n]                                 if    year==`DECADE'  & black==1 
predict                        x1                                                               if    year==`DECADE'  & black==1  &  e(sample),  residual
replace                        black_residual = x1                                              if    year==`DECADE'  & black==1  &  e(sample)
ereturn clear
drop x1    
}



global    MEN_FIG_LIST  " mfg  lincwage      emprate      emprate_noncog     nevermar        poor      lvalueh   educ_n   COG      " 

foreach varb1 in $MEN_FIG_LIST  {
gen      black_`varb1' = `varb1'   if racesing==2
gen      white_`varb1' = `varb1'   if racesing==1
}

collapse     black_*  white_*   $MEN_FIG_LIST  (p10) white_lincwage_10 = white_lincwage   (p50) white_lincwage_50   =  white_lincwage    (p90) white_lincwage_90 = white_lincwage   (p10) black_lincwage_10 = black_lincwage   (p50) black_lincwage_50   =  black_lincwage    (p90) black_lincwage_90 = black_lincwage    (p10) white_residual_10 = white_residual   (p50) white_residual_50   =  white_residual    (p90) white_residual_90 = white_residual   (p10) black_residual_10 = black_residual   (p50) black_residual_50   =  black_residual    (p90) black_residual_90 = black_residual    [aw=perwt], by(year)


gen  white_9010  =  white_lincwage_90 - white_lincwage_10
gen  black_9010  =  black_lincwage_90 - black_lincwage_10

gen  white_res_9010  =  white_residual_90 - white_residual_10
gen  black_res_9010  =  black_residual_90 - black_residual_10

format   white_mfg   black_mfg  mfg    white_lincwage  black_lincwage      white_emprate  black_emprate   white_emprate_noncog  black_emprate_noncog    black_nevermar  white_nevermar    black_poor  white_poor  black_COG  white_COG       white_9010 black_9010  white_res_9010  black_res_9010    white_educ_n   black_educ_n        %9.3f



global FIG_NUMBER "1"
twoway       (connected mfg year, mlabel(mfg) mlabsize(small) mlabp(6 6))     (connected black_mfg year, lpattern(dash)  msymbol(triangle)  mlabel(black_mfg) mlabsize(small) mlabp(6 6)),   ///
title("Figure ${FIG_NUMBER}: Employment Share in Manufacturing" , size(medlarge))  subtitle("Males - 25-55 Years Old",size(medsmall))                             ///
xtitle("Year")     xscale(range(1960 2015))   ///
legend(region(lstyle(none)) label(1 All Men) label(2 Black Men )  )  ///
caption("Notes: The sample includes native born men between the ages of 25 and 55."  , size(vsmall)) ///
ytitle("MFG Employment Share", size(small))  ylabel(0(0.1)0.3) ylabel(,labsize(small) format(%9.2f)) 
graph save Graph  Figure_${FIG_NUMBER}, replace



global FIG_NUMBER "A1"
twoway       (connected white_nevermar year, mlabel(white_nevermar) mlabsize(small) mlabp(6 6))     (connected black_nevermar year, lpattern(dash) msymbol(triangle)  mlabel(black_nevermar) mlabsize(small) mlabp(6 6)),   ///
title("Figure ${FIG_NUMBER}: Percent Never Married for Men" , size(medlarge))  subtitle("Males - 25-55 Years Old",size(medsmall))                             ///
xtitle("Year")     xscale(range(1960 2015))   ///
legend(region(lstyle(none)) label(1 White Men) label(2 Black Men )  )  ///
caption("Notes: The sample includes native born men between the ages of 25 and 55."  , size(vsmall)) ///
ytitle("Percent Never Married", size(small))  ylabel(0.0(0.2)0.5) ylabel(,labsize(small) format(%9.1f)) 
graph save Graph  Figure_${FIG_NUMBER}, replace


global FIG_NUMBER "2"
twoway       (connected white_9010 year, mlabel(white_9010) mlabsize(small) mlabp(6 6))     (connected black_9010 year, lpattern(dash) msymbol(triangle)  mlabel(black_9010) mlabsize(small) mlabp(6 6)),   ///
title("Figure ${FIG_NUMBER}: 90/10 Wage Ratio for Men" , size(medlarge))  subtitle("Males - 25-55 Years Old",size(medsmall))                             ///
xtitle("Year")     xscale(range(1960 2015))   ///
legend(region(lstyle(none)) label(1 White Men) label(2 Black Men )  )  ///
caption("Notes: The sample includes native born men between the ages of 25 and 55."  , size(vsmall)) ///
ytitle("90/10 Ratio of Log Wages", size(small))  ylabel(1.0(0.2)1.6) ylabel(,labsize(small) format(%9.1f)) 
graph save Graph  Figure_${FIG_NUMBER}, replace

restore





***************************************
*  GENERATE MAIN VARBS FOR WHITE MEN  *
***************************************

preserve
keep if sex==1
keep if racesing==1
drop if $FE_LEVEL==0

collapse  $VARBLIST  $VARB_DEMOS (count) nobs=lincwage  (sd) std_hvalue=lvalueh   [aw=perwt] , by($FE_LEVEL  year)
sort $FE_LEVEL  year
save ${FE_NAME}_white_men_mainvarbs, replace
restore


***************************************
*  GENERATE MAIN VARBS FOR BLACK MEN  *
***************************************

preserve
keep if sex==1
keep if racesing==2
drop if $FE_LEVEL==0


foreach varb1 in $VARBLIST  $VARB_DEMOS  {
rename  `varb1'  black_`varb1'
}
collapse  black_*  (count) black_nobs=black_lincwage  (sd) black_std_hvalue=black_lvalueh    [aw=perwt], by($FE_LEVEL  year)
sort $FE_LEVEL  year
save ${FE_NAME}_black_men_mainvarbs, replace
restore


***************************************
*  GENERATE MAIN VARBS FOR ALL MEN  *
***************************************

preserve
keep if sex==1
drop if $FE_LEVEL==0


foreach varb1 in $VARB_DEMOS   {
rename  `varb1'  all_men_`varb1'
}
collapse  all_men_*     [aw=perwt], by($FE_LEVEL  year)
sort $FE_LEVEL  year
save ${FE_NAME}_all_men_mainvarbs, replace
restore





*********************************
*  BARTIK MFG FOR WHITE MEN  
*********************************

preserve
keep if sex==1
keep if racesing==1
keep if gq==1
keep if studying==0

gen one=perwt
egen sample_state_year=total(one), by ($FE_LEVEL year)
egen sample_year=total(one), by (year)

egen emp_ind_state_year =  total(one) , by ($FE_LEVEL year industry)
egen emp_ind_year       =  total(one) , by (year industry)

gen sample_ex          =   sample_year -  sample_state_year
gen emp_ex             =   emp_ind_year - emp_ind_state_year

gen emp_share     =   emp_ind_state_year/sample_state_year
gen emp_share_ex  =   emp_ex/sample_ex

global BASE_YEAR "1960 1970 1980 1990"
foreach year in $BASE_YEAR {
gen emp_share_base_temp_`year'=emp_share if year==`year'
bysort $FE_LEVEL industry : egen emp_share_base_`year'=max(emp_share_base_temp_`year') if year>=`year'
gen bartik_`year'=emp_share_base_`year'*emp_share_ex 
replace bartik_`year'=0 if industry_temp=="." & bartik_`year'!=.

}

foreach indcode of numlist  2/17 {

gen temp_indshare_base_1960_`indcode' = 0
gen  x_1 =  emp_ind_year/sample_year  if industry==`indcode'
egen x_2 =  max(x_1), by(year)
gen temp_indshare_overtime_`indcode'  = x_2
drop x_*
}

foreach indcode of numlist  2/17 {

replace temp_indshare_base_1960_`indcode' = emp_share_base_1960  if industry==`indcode'
replace temp_indshare_overtime_`indcode'  = emp_share_ex         if industry==`indcode'

bysort $FE_LEVEL year : egen indshare_base_1960_`indcode' = max(temp_indshare_base_1960_`indcode') if year>=1960
bysort $FE_LEVEL year : egen indshare_overtime_`indcode'  = max(temp_indshare_overtime_`indcode')  
}
drop temp_*

gen bartik_IV_1960_2 = 0
foreach indcode of numlist  2/17 {
replace bartik_IV_1960_2 = bartik_IV_1960_2 + indshare_base_1960_`indcode'*indshare_overtime_`indcode'
}

egen rtot_1960 = rowtotal(indshare_base_1960_*)
replace bartik_IV_1960_2 = . if rtot_1960==0 
foreach indcode of numlist  2/17 {
replace indshare_base_1960_`indcode' = .  if rtot_1960==0
replace indshare_overtime_`indcode'  = .  if rtot_1960==0
}
drop rtot_*

foreach year in $BASE_YEAR {
duplicates drop $FE_LEVEL year industry, force

egen bartik_IV_mfg_`year'=total(bartik_`year') if year>=`year', by ($FE_LEVEL year)   missing
}

duplicates drop $FE_LEVEL year, force
keep  bartik_IV_mfg_*  $FE_LEVEL year      indshare_*  bartik_IV_1960_2  
sort  $FE_LEVEL year
save ${FE_NAME}_white_men_bartik_mfg, replace

restore





***************************************************
*               INEQUALITY MEASURES - WHITE MEN  
***************************************************


preserve
keep if sex==1
keep if racesing==1
keep if native==1
keep if gq==1
keep if studying==0
keep if selfemp==0
keep if lincwage~=.
drop if $FE_LEVEL==0

gen res_educ = .
gen res_occ_ind = .
gen res_metro = .



foreach year of numlist 1960 1970 1980 1990 2000 2010  {

xi: regress lincwage  i.educ_group*i.age_group                                           if year==`year' 
predict resid, residuals
replace res_educ = resid if year==`year'
drop resid

xi: regress lincwage  i.educ_group*i.age_group  i.occ_group  i.ind_group                 if year==`year' 
predict resid, residuals
replace res_occ_ind = resid if year==`year'
drop resid

xi: regress  lincwage  i.educ_group*i.age_group  i.occ_group  i.ind_group   i.$FE_LEVEL  if year==`year' 
predict resid, residuals
replace res_metro = resid if year==`year'
drop resid

}


collapse    (p10) tot_lincwage_10 = lincwage             (p50) tot_lincwage_50   =  lincwage        (p90) tot_lincwage_90 = lincwage         ///
            (p10) res_educ_10     = res_educ             (p50) res_educ_50       =  res_educ        (p90) res_educ_90     = res_educ        ///
            (p10) res_occ_ind_10  = res_occ_ind          (p50) res_occ_ind_50    =  res_occ_ind     (p90) res_occ_ind_90  = res_occ_ind     ///
            (p10) res_metro_10    = res_metro            (p50) res_metro_50      =  res_metro       (p90) res_metro_90    = res_metro       ///
[fw=perwt], by ($FE_LEVEL year) 
			
			
gen tot_9010 =  tot_lincwage_90 -  tot_lincwage_10			
gen tot_9050 =  tot_lincwage_90 -  tot_lincwage_50			
gen tot_5010 =  tot_lincwage_50 -  tot_lincwage_10			
			
gen res_educ_9010 =  res_educ_90 -  res_educ_10			
gen res_educ_9050 =  res_educ_90 -  res_educ_50			
gen res_educ_5010 =  res_educ_50 -  res_educ_10			

gen res_occ_ind_9010 =  res_occ_ind_90 -  res_occ_ind_10			
gen res_occ_ind_9050 =  res_occ_ind_90 -  res_occ_ind_50			
gen res_occ_ind_5010 =  res_occ_ind_50 -  res_occ_ind_10			
			
gen res_metro_9010 =  res_metro_90 -  res_metro_10			
gen res_metro_9050 =  res_metro_90 -  res_metro_50			
gen res_metro_5010 =  res_metro_50 -  res_metro_10			


global  INEQ_VARBLIST  "tot_9010   tot_9050  tot_5010   tot_lincwage_90   tot_lincwage_50  tot_lincwage_10			res_educ_9010   res_educ_9050     res_educ_5010   res_educ_90   res_educ_50	  res_educ_10        res_occ_ind_90   res_occ_ind_50	 res_occ_ind_10     res_occ_ind_9010   		res_occ_ind_9050   	res_occ_ind_5010       res_metro_90  res_metro_50	 res_metro_10		res_metro_9010   res_metro_9050   	res_metro_5010   "
keep  $FE_LEVEL  year  $INEQ_VARBLIST
sort  $FE_LEVEL year
save ${FE_NAME}_white_men_ineq, replace
restore


***************************************************
*               INEQUALITY MEASURES - BLACK MEN  
***************************************************


preserve
keep if sex==1
keep if racesing==2
keep if native==1
keep if gq==1
keep if studying==0
keep if selfemp==0
keep if lincwage~=.
drop if $FE_LEVEL==0

gen res_educ = .
gen res_occ_ind = .
gen res_metro = .



foreach year of numlist 1960 1970 1980 1990 2000 2010  {

xi: regress lincwage  i.educ_group*i.age_group                                           if year==`year' 
predict resid, residuals
replace res_educ = resid if year==`year'
drop resid

xi: regress lincwage  i.educ_group*i.age_group  i.occ_group  i.ind_group                 if year==`year' 
predict resid, residuals
replace res_occ_ind = resid if year==`year'
drop resid

xi: regress  lincwage  i.educ_group*i.age_group  i.occ_group  i.ind_group   i.$FE_LEVEL  if year==`year' 
predict resid, residuals
replace res_metro = resid if year==`year'
drop resid

}


collapse    (p10) tot_lincwage_10 = lincwage             (p50) tot_lincwage_50   =  lincwage        (p90) tot_lincwage_90 = lincwage         ///
            (p10) res_educ_10     = res_educ             (p50) res_educ_50       =  res_educ        (p90) res_educ_90     = res_educ        ///
            (p10) res_occ_ind_10  = res_occ_ind          (p50) res_occ_ind_50    =  res_occ_ind     (p90) res_occ_ind_90  = res_occ_ind     ///
            (p10) res_metro_10    = res_metro            (p50) res_metro_50      =  res_metro       (p90) res_metro_90    = res_metro       ///
[fw=perwt], by ($FE_LEVEL year) 
			
			
gen tot_9010 =  tot_lincwage_90 -  tot_lincwage_10			
gen tot_9050 =  tot_lincwage_90 -  tot_lincwage_50			
gen tot_5010 =  tot_lincwage_50 -  tot_lincwage_10			
			
gen res_educ_9010 =  res_educ_90 -  res_educ_10			
gen res_educ_9050 =  res_educ_90 -  res_educ_50			
gen res_educ_5010 =  res_educ_50 -  res_educ_10			

gen res_occ_ind_9010 =  res_occ_ind_90 -  res_occ_ind_10			
gen res_occ_ind_9050 =  res_occ_ind_90 -  res_occ_ind_50			
gen res_occ_ind_5010 =  res_occ_ind_50 -  res_occ_ind_10			
			
gen res_metro_9010 =  res_metro_90 -  res_metro_10			
gen res_metro_9050 =  res_metro_90 -  res_metro_50			
gen res_metro_5010 =  res_metro_50 -  res_metro_10			




foreach varb1 in $INEQ_VARBLIST {
rename  `varb1'  black_`varb1'
}

keep  $FE_LEVEL  year  black_*
sort  $FE_LEVEL year
save ${FE_NAME}_black_men_ineq, replace

restore





***********************************************************************
*   MEN  ---  BLACK WHITE GAP, ROR TO COLLEGE FOR WHITES AND BLACKS
***********************************************************************

keep if sex==1
keep if native==1

drop if $FE_LEVEL==0
drop if black==0 & white==0

gen x_1 = lincwage  if black==1
egen  black_nobs=count(x_1), by($FE_LEVEL  year)
drop if black_nobs<40
drop x_1 black_nobs


global  VARB_GAPX_MEN         "lincwage                 nevermar        emprate  lvalueh  groupq   poor   welfare_dummy  "

foreach  varb1  in  $VARB_GAPX_MEN  {
gen           gapx_`varb1' = .
gen            ror_`varb1' = .
gen      black_ror_`varb1' = .
}


foreach DECADE of numlist 1960 1970 1980 1990 2000 2010  {
di "hello world"
preserve
keep if year==`DECADE'
levelsof $FE_LEVEL, local(levels)
foreach GEO in  `levels' {
foreach  varb1  in  $VARB_GAPX_MEN  {
cap noi   regress  `varb1'          educ_n  age  i.age_group           [aw=perwt]             if    $FE_LEVEL==`GEO' & year==`DECADE'  & white==1
cap noi   replace                   ror_`varb1' =  _b[educ_n]                                 if    $FE_LEVEL==`GEO' & year==`DECADE'  & white==1 
ereturn clear                                                   
cap noi   regress  `varb1'          educ_n  age  i.age_group           [aw=perwt]             if    $FE_LEVEL==`GEO' & year==`DECADE'  & black==1
cap noi   replace             black_ror_`varb1' =  _b[educ_n]                                 if    $FE_LEVEL==`GEO' & year==`DECADE'  & black==1 
ereturn clear                                                   
}


foreach  varb1  in  $VARB_GAPX_MEN  {
cap noi   regress `varb1'       HSD COD COG  age  i.age_group   black   [aw=perwt]            if    $FE_LEVEL==`GEO' & year==`DECADE'
cap noi   replace               gapx_`varb1' =  _b[black]                                     if    $FE_LEVEL==`GEO' & year==`DECADE'
ereturn clear                                                   
}
}
keep  $FE_LEVEL  year  gapx_*  black_ror_*  ror_*
collapse  gapx_*  black_ror_*  ror_* , by($FE_LEVEL year)
sort  $FE_LEVEL  year
save  SCRAP_ROR_`DECADE' , replace
restore
}

bysort year: sum age lincwage  COG

clear
use  SCRAP_ROR_1960
merge  $FE_LEVEL  year   using SCRAP_ROR_1970  SCRAP_ROR_1980  SCRAP_ROR_1990  SCRAP_ROR_2000  SCRAP_ROR_2010
tab  _merge
drop _merge
sort  $FE_LEVEL  year
save ${FE_NAME}_ROR_gapx_men, replace
 

